package com.zyh.server.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author: zyh
 * @Date: 2024-11-13-9:39
 * @Description Redis配置类
 */
@Configuration
public class RedisConfig {
	//redis 非关系数据库，key-value
	// 序列化:将对象永久存储到网络或者磁盘的过程
	// RedisTemplate 操作redis数据库工具类
	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
		//设置连接redis
		redisTemplate.setConnectionFactory(factory);
		//设置key的序列化器，一般是String类型
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//设置value的序列化器(转换json格式的字符串)
		ObjectMapper mapper = new ObjectMapper();
		Jackson2JsonRedisSerializer jsonRedisSerializer =new Jackson2JsonRedisSerializer(mapper, Object.class);
		redisTemplate.setValueSerializer(jsonRedisSerializer);
		return redisTemplate;
	}
}
